package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMPlanTypeRelation;

public class ServiceZMCMI0 extends ServiceEPBase 
{
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cm.planType", true));//构造计划类型下拉框
		outInfo.addBlock(generateDefaultPlanType());

		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-25上午11:11:35 bobswang
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCMPlanTypeRelation.query",new ZMCMPlanTypeRelation());
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-25上午11:12:15 bobswang
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo) 
	{
		String result = validate(inInfo, false);
		
		if(!result.equals("success"))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg(result);
			return inInfo;
		}
		
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_PLANTYPERELATION"));//主键
		EiInfo outInfo = super.insert(inInfo, "ZMCMPlanTypeRelation.insert");
		return outInfo;
	}

	

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-25上午11:13:20 bobswang
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		String result = validate(inInfo, true);
		
		if(!result.equals("success"))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg(result);
			return inInfo;
		}
		
		EiInfo outInfo = super.update(inInfo, "ZMCMPlanTypeRelation.update");
		return outInfo;
	}
	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-25上午11:13:32 bobswang
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo)
	{		
		EiInfo outInfo = super.delete(inInfo, "ZMCMPlanTypeRelation.delete");
		
		if(outInfo.getStatus() != -1)
		{
			outInfo = this.query(inInfo);
			return outInfo;
		}
		
		return outInfo;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private String validate(EiInfo inInfo, boolean updateFlag)
	{
		String fdDefaultTypeName = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdDefaultTypeName");
		String fdUserTypeName = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdUserTypeName");
		String fdUserTypeName1 = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdUserTypeName1");
		String fdUserTypeName2 = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdUserTypeName2");
		
		if(fdUserTypeName.equals(fdUserTypeName1) || fdUserTypeName.equals(fdUserTypeName2))
		{
			return "选择的用户设置计划名称重复，请重新选择！";
		}
		
		if((fdUserTypeName1 != null && !"".equals(fdUserTypeName1)))
		{
			if(fdUserTypeName1.equals(fdUserTypeName2))
			{
				return "选择的用户设置计划名称重复，请重新选择！";
			}
		}
		
		HashMap inMap = new HashMap();
		if(updateFlag)
		{
			inMap.put("fdKeyId", inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdKeyId"));
		}
		
		inMap.put("fdDefaultTypeName", fdDefaultTypeName);
		List _list = dao.query("ZMCMPlanTypeRelation.validate", inMap);
		if(_list.size() > 0)
		{
			return "选择的系统默认计划类型已经存在，不能新建重复的系统默认计划类型！";
		}
		
		inMap.remove("fdDefaultTypeName");
		inMap.put("fdUserTypeName", fdUserTypeName);
		_list = dao.query("ZMCMPlanTypeRelation.validate", inMap);
		if(_list.size() > 0)
		{
			return "选中的用户设置计划名称已经存在，不能新建重复的用户设置计划名称！";
		}
		
		if(fdUserTypeName1 != null && !"".equals(fdUserTypeName1))
		{
			inMap.put("fdUserTypeName", fdUserTypeName1);
			_list = dao.query("ZMCMPlanTypeRelation.validate", inMap);
			if(_list.size() > 0)
			{
				return "选中的用户设置计划名称1已经存在，不能新建重复的用户设置计划名称1！";
			}
		}
		
		if(fdUserTypeName2 != null && !"".equals(fdUserTypeName2))
		{
			inMap.put("fdUserTypeName", fdUserTypeName2);
			_list = dao.query("ZMCMPlanTypeRelation.validate", inMap);
			if(_list.size() > 0)
			{
				return "选中的用户设置计划名称2已经存在，不能新建重复的用户设置计划名称2！";
			}
		}
		
		return "success";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private EiBlock generateDefaultPlanType()
	{
		EiBlock block = new EiBlock("cmes.cm.defaultPlanType");
		EiColumn column = new EiColumn("value");
		column.setDescName("值");
		block.getBlockMeta().addMeta(column);
		
		column = new EiColumn("display");
		column.setDescName("名称");
		block.getBlockMeta().addMeta(column);
		
		Map inMap = new HashMap();
		inMap.put("value", "月度计划");
		inMap.put("display", "月度计划");
		block.addRow(inMap);
		
		inMap = new HashMap();
		inMap.put("value", "中旬计划");
		inMap.put("display", "中旬计划");
		block.addRow(inMap);
		
		inMap = new HashMap();
		inMap.put("value", "急用计划");
		inMap.put("display", "急用计划");
		block.addRow(inMap);
		
		return block;
	}
}
